package com.ktjiaoyu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktjiaoyu.server.mapper.AdminRoleMapper;
import com.ktjiaoyu.server.mapper.MenuRoleMapper;
import com.ktjiaoyu.server.mapper.RoleMapper;
import com.ktjiaoyu.server.pojo.Admin;
import com.ktjiaoyu.server.pojo.AdminRole;
import com.ktjiaoyu.server.pojo.MenuRole;
import com.ktjiaoyu.server.pojo.Role;
import com.ktjiaoyu.server.service.IAdminRoleService;
import com.ktjiaoyu.server.service.IRoleService;
import com.ktjiaoyu.server.vo.AdminRoleVo;
import com.ktjiaoyu.server.vo.RoleMenuVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jjx
 * @since 2024-07-10
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Resource
    RoleMapper roleMapper;
    @Resource
    AdminRoleMapper adminRoleMapper;
    @Resource
    MenuRoleMapper menuRoleMapper;

    @Override
    public IPage<Role> selectByPage(Page<Role> page, String query, String sort) {
        QueryWrapper<Role> qw=new QueryWrapper<>();
        qw.like("name",query)
                .or()
                .like("nameZh",query);
        Page<Role> rolePage = roleMapper.selectPage(page, qw);
        return rolePage;
    }

    @Override
    public boolean saveRole(Role role) {
        int rs = roleMapper.insert(role);
        if(rs>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateRole(Role role) {
        int rs = roleMapper.updateById(role);
        if(rs>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delRoleById(Integer roleId) {
        int rs=roleMapper.deleteById(roleId);
        if(rs>0){
            return true;
        }
        return false;
    }

    @Override
    public List<Role> roleList() {
        return roleMapper.selectList(null);
    }

    @Override
    public boolean batchInsertAdminRole(AdminRoleVo vo) {
        try {
            //删除
            QueryWrapper<AdminRole>  qw=new QueryWrapper<>();
            qw.eq("adminId",vo.getAdminId());
            adminRoleMapper.delete(qw);

            //新增
            if(vo.getRid()!=null && vo.getRid().length>0){
                for (Integer roleId : vo.getRid()) {
                    AdminRole adminRole=new AdminRole();
                    adminRole.setAdminId(vo.getAdminId());
                    adminRole.setRid(roleId);

                    adminRoleMapper.insert(adminRole);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public AdminRoleVo getAdminRoleList(Integer adminId) {
        //构造查询条件
        QueryWrapper<AdminRole> qw=new QueryWrapper<>();
        qw.eq("adminId",adminId);
        //执行查询
        List<AdminRole> list = adminRoleMapper.selectList(qw);

        if(list==null || list.size()==0){//如果没有分配过角色
            return null;
        }

        List<Integer> roleIds=new ArrayList<>();
        for (AdminRole adminRole : list) {
            roleIds.add(adminRole.getRid());
        }
        AdminRoleVo adminRoleVo=
                new AdminRoleVo(adminId,roleIds.toArray(new Integer[roleIds.size()]));
        return adminRoleVo;
    }

    @Override
    public boolean batchInsertRoleMenu(RoleMenuVo vo) {
        try {
            //删除
            QueryWrapper<MenuRole> qw=new QueryWrapper<MenuRole>();
            qw.eq("rid", vo.getRid());
            menuRoleMapper.delete(qw);

            //新增
            if(vo.getMid()!=null && vo.getMid().length>0){
                for (Integer mid : vo.getMid()) {
                    MenuRole menuRole=new MenuRole();
                    menuRole.setRid(vo.getRid());
                    menuRole.setMid(mid);

                    menuRoleMapper.insert(menuRole);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
